local yangming = fk.CreateSkill {
  name = "yangming",
}

yangming:addEffect("active", {
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#yangming",
  can_use = function(self, player)
    return not player:isKongcheng() and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter =function (self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player and player:canPindian(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    while not (player.dead or target.dead) and player:canPindian(target) do
      local pindian = player:pindian({target}, self.name)
      if pindian.results[target.id].winner ~= target then
        if not player.dead and not target.dead and player:canPindian(target)
        and room:askToSkillInvoke(player,{skill_name=self.name,prompt="#yangming-invoke::"..target.id})then
          player:broadcastSkillInvoke(self.name)
          room:notifySkillInvoked(player, self.name)
          room:doIndicate(player.id, {target.id})
        else
          break
        end
      else
        if not target.dead then
          local n = #room.logic:getEventsOfScope(GameEvent.Pindian, 999, function (e)
            local dat = e.data
            return dat.results[target.id] and dat.results[target.id].winner ~= target
          end, Player.HistoryPhase)
          if n > 0 then
            target:drawCards(n, self.name)
          end
        end
        if not player.dead and player:isWounded() then
          room:recover{
            who = player,
            num = 1,
            recoverBy = player,
            skillName = self.name
          }
        end
        break
      end
    end
  end,
})

return yangming